package chaw.android.train;


import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import chaw.android.train.MoreActivities.MoreSpecialActivity;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;

public class StationScheduleActivity extends Activity {
	private WebView stationScheduleWebView, bannerWebView;
	private WebViewClient client;
	private ImageView btnBack;
	private ImageButton board, people, info, write;
	private TextView station;
	private String stationId, station_name;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.station_schedule);
		
		stationScheduleWebView = (WebView) findViewById(R.id.moreHelpWebView);
        client = new WebViewClient();
		String url = ServerURL.stationSchedule;
		Intent intent = getIntent();
		stationId = intent.getExtras().get("station_id").toString();
		station_name = intent.getExtras().get("station_name").toString();
	    bannerWebView = (WebView) findViewById(R.id.banner);
		bannerWebView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url){
				String[] values = url.split("!");
				if(values[1].equals("app")){
					if(values[2].equals("live")){
						Intent intent = new Intent(getBaseContext(), AllBoardActivity.class);
						startActivity(intent);
						
					}
					else if(values[2].equals("enjoy")){
						Intent intent = new Intent(getBaseContext(), MoreSpecialActivity.class);
						startActivity(intent);
					}
				}
				else if(values[1].equals("board")){
					ActivityGroup parentActivity = (ActivityGroup) getParent();
					Intent intent = new Intent(getBaseContext(), BoardViewActivity.class);
					intent.putExtra("board_id", values[2]);
					intent.putExtra("station_id", stationId);
					intent.putExtra("station_name", station_name);
					((FavoriteActivity) parentActivity).startChildActivity("BoardView", intent);
				}
				else if(values[1].equals("web")){
					Intent intent = new Intent(Intent.ACTION_VIEW); 
					Uri uri = Uri.parse(values[2]); 
					intent.setData(uri); 
					startActivity(intent);
				}
				return true;
			}
		});
		bannerWebView.loadUrl(ServerURL.bannerWebView(stationId,
													  Preferences.getValue(getBaseContext(), "device_token"),
													  Preferences.getValue(getBaseContext(), "user_id"),
													  Preferences.getValue(getBaseContext(), "version")));
		station = (TextView)findViewById(R.id.stationName);
		board = (ImageButton)findViewById(R.id.btnShowProfile);
		people = (ImageButton)findViewById(R.id.btnShowPeople);
		info = (ImageButton)findViewById(R.id.btnShowStationAbout);
		write = (ImageButton)findViewById(R.id.add_board);
		stationScheduleWebView.setWebViewClient(client);
		stationScheduleWebView.loadUrl(url+stationId);
		station.setText(station_name + "역");
		
		btnBack = (ImageView) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				FavoriteActivity parentActivity = (FavoriteActivity)getParent();
				parentActivity.finishFromChild(StationScheduleActivity.this);
			}
		});

		board.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				ActivityGroup parentActivity = (ActivityGroup) getParent();
				Intent intent = new Intent(getBaseContext(), StationBoardActivity.class);
				intent.putExtra("station_id", stationId);
				intent.putExtra("station_name", station_name);
				((FavoriteActivity) parentActivity).startChildActivityWithoutCurrentActivity("StationBoard", intent);
			}
		});
		people.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				ActivityGroup parentActivity = (ActivityGroup) getParent();
				Intent intent = new Intent(getBaseContext(), StationPeopleActivity.class);
				intent.putExtra("station_id", stationId);
				intent.putExtra("station_name", station_name);
				((FavoriteActivity) parentActivity).startChildActivityWithoutCurrentActivity("StationPeople", intent);
			}
		});
		info.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				ActivityGroup parentActivity = (ActivityGroup) getParent();
				Intent intent = new Intent(getBaseContext(), StationInfoActivity.class);
				intent.putExtra("station_id", stationId);
				intent.putExtra("station_name", station_name);
				((FavoriteActivity) parentActivity).startChildActivityWithoutCurrentActivity("StationInfo", intent);
			}
		});
		write.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getBaseContext(), BoardWriteActivity.class);
				intent.putExtra("station_id", stationId);
				intent.putExtra("station_name", station_name);
				startActivity(intent);
				}
		});
		
	}
}
